$(function() {
    var successMsgContainer = $("div[id*=successMessageContainer]");
    var errorMsgContainer = $("div[id*=errorMessageContainer]");
    var successMsg = $("span[id*=successMessage]");
    var errorMsg = $("span[id*=errorMessage]");
    if (successMsg.text()) successMsgContainer.show();
    if (errorMsg.text()) errorMsgContainer.show();
      
    var btnSaveGrades = $("button[id*=saveGrades]");
    btnSaveGrades.click(validateGrades);
    
    var maxPoints = parseInt($("[id*=maxPoints]").val());
    var newPoints = $("[id*=pointsEarned_newGrade]");
    var newComments = $("[id*=comments_newGrade]");
    var editedPoints = $("[id*=pointsEarned_editableGrade]");
    var editedComments = $("[id*=comments_editableGrade]");

    var prevChkbxIdCourse = "";
    var prevChkbxIdProf = "";
    $("button[id*=editMode]").click(toggleEditMode); 
    //toggling for course check boxes
    function toggleCourse(chkBx) {//make sure only one checkbox is selected
        //  alert("he");
        if (prevChkbxIdCourse !== "" && prevChkbxIdCourse !== chkBx.id) {
            document.getElementById(prevChkbxIdCourse).checked = false;
        }
        prevChkbxIdCourse = chkBx.id;
    }

    //toggling for prof check boxes
    function toggleProf(chkBx) {
        if (prevChkbxIdProf !== "" && prevChkbxIdProf !== chkBx.id) { //do not do this on first check
            document.getElementById(prevChkbxIdProf).checked = false;
        }
        prevChkbxIdProf = chkBx.id;
    }

    function toggleEditMode() {
        var button = this;
            
        if (button.value === "Activate Edit Mode") {
            button.value = "De-activate Edit Mode";
            btnSaveGrades.removeAttr("disabled");
            btnSaveGrades.addClass("btn-primary");
            btnSaveGrades.removeClass("btn-disabled");
            if (editedPoints) editedPoints.removeAttr("disabled");
            if (editedComments) editedComments.removeAttr("disabled");
            if (newPoints) newPoints.removeAttr("disabled");
            if (newComments) newComments.removeAttr("disabled");
        }
        else {
            button.value = "Activate Edit Mode";
            btnSaveGrades.attr("disabled", "disabled");
            btnSaveGrades.removeClass("btn-primary");
            btnSaveGrades.addClass("btn-disabled");
            if (editedPoints) editedPoints.attr("disabled", "disabled");
            if (editedComments) editedComments.attr("disabled", "disabled");
            if (newPoints) newPoints.attr("disabled", "disabled");
            if (newComments) newComments.attr("disabled", "disabled");
        }
    }
    
    function validateGrades() {
        var hasErrors = parseInputs(newPoints) || parseInputs(editedPoints);
        if (hasErrors) {
            errorMsg.text('Grade(s) with points greather than the max points.');
            errorMsgContainer.show();
        }
        return !hasErrors;
    }
    
    function parseInputs(inputs) {
        var hasErrors = false;
        $.each(inputs, function (i, point) {
            var value = ($(point).val() === "") ? 0 : parseInt($(point).val());
            if ((value !== 0) && (value > maxPoints)) {
                hasErrors = true;
                $(point).parent().addClass('danger');
            }
            else {
                $(point).parent().removeClass('danger');
            }
        });
        
        return hasErrors;
    }
});